package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    /**
     * 新增菜品
     * @param dto
     */
    @Transactional  //涉及到多张表的增删改操作，需要开事务
    public void addDish(DishDTO dto) {
        //1.构造菜品基本信息，将其存入dish表中
        Dish dish = new Dish();
        //将dto中的属性复制到dish对象中
        BeanUtils.copyProperties(dto, dish);
        //调用mapper的insert方法，将dish对象插入到数据库中
        dishMapper.insert(dish);
        //2.构造菜品口味列表数据，将其存入dish_flavor表中
        List<DishFlavor> flavors = dto.getFlavors();
        //2.1关联菜品id
        if(flavors !=null && flavors.size()>0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper获取对象
        Page<Dish> page = dishMapper.page(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询菜品 也需要连表查询
     * @param id
     * @return
     */
    @Transactional //开启事务
    public DishDTO getById(Long id) {
        DishDTO dishDTO = new DishDTO();
        DishDTO DishById = dishMapper.getById(id);
        BeanUtils.copyProperties(DishById, dishDTO);
        List<DishFlavor> dishFlavors = dishFlavorMapper.listByDishId(id);
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    /**
     * 修改菜品，此处也需要去更新两个表
     * @param dto
     */
    @Transactional //开启事务
    public void update(DishDTO dto) {
        Dish dish = new Dish();
        //将前端的字段传入到dish对象中
        BeanUtils.copyProperties(dto, dish);
        log.info("id为：{}",dish.getId());
        dishMapper.update(dish); //此处更新了dish表
        //先进行删除，然后在进行添加
        List<DishFlavor> dishFlavors = dishFlavorMapper.listByDishId(dish.getId());
        if (!dishFlavors.isEmpty()){
            dishFlavorMapper.deleteByDishId(dish.getId());
        }
        //从前端传入的口味列表重取出数据
        List<DishFlavor> flavors = dto.getFlavors();
        if(!flavors.isEmpty()){
            //循环前前端传入的口味列表设置DishId
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            //最后在调用mapper的批量插入方法，将前端的数据插入到dish_flavor表中
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void deleteBatch(String ids) {
        //1.首先需要将ids转化成一个列表，然后调用mapper进行循环删除
        List<String> idList = Arrays.asList(ids.split(","));
        dishMapper.deleteBatch(idList);

    }

    /**
     * 根据种类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByCategoryId(Integer categoryId) {
            return dishMapper.getByCategoryId(categoryId);
    }

    /**
     * 菜品的停售起售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish  = Dish.builder()
                    .status( status)
                    .id(id)
                    .build();
        dishMapper.update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.ListBy(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.listByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
